/*
 * @Description:
 * @Version: 1.0
 * @Autor: laikt
 * @Date: 2023-07-06 21:07:02
 * @LastEditors: laikt
 * @LastEditTime: 2023-09-08 14:32:14
 */
import { Inject, Injectable } from '@nestjs/common'
import * as dotenv from 'dotenv'
import * as fs from 'fs'
import * as path from 'path'
import { CONFIG_OPTIONS } from './constants'
import { ConfigOptions, EnvConfig } from './interfaces'

@Injectable()
export class ConfigService {
    private readonly envConfig: EnvConfig

    constructor(@Inject(CONFIG_OPTIONS) options: ConfigOptions) {
        const filePath = `${process.env.NODE_ENV || 'development'}.env`
        const envFile = path.resolve(
            __dirname,
            '../../',
            options.folder,
            filePath
        )
        this.envConfig = dotenv.parse(fs.readFileSync(envFile))
        console.log(this.envConfig)
    }

    public get(key: string): string {
        return this.envConfig[key]
    }
    public getNumber(key: string): number {
        return Number(this.envConfig[key])
    }
    public getBoolean(key: string): boolean {
        return JSON.parse(this.envConfig[key])
    }
}
